package org.sunrise.game.game.server;

import lombok.Getter;
import org.sunrise.game.common.network.client.BaseClient;

import java.util.Map;
import java.util.Objects;

public class GameManager {
    @Getter
    private static GameClient gameClient;
    public static GameClient createGameClient(int serverId) {
        gameClient = new GameClient(serverId);
        return gameClient;
    }

    public static int getGameId() {
        if (gameClient != null) {
            return gameClient.getServerId();
        }
        return 0;
    }

    /**
     * 通过连接的对外服id获取此客户端的节点id
     */
    public static String getConnectToExternalNodeIdById(int externalId) {
        if (gameClient != null) {
            BaseClient baseClient = gameClient.getConnectToExternals().get(externalId);
            if (baseClient != null) {
                return baseClient.getNodeId();
            }
        }
        return "";
    }

    /**
     * 通过连接的对外服节点id获取此对外服的id
     */
    public static int getConnectToExternalIdByNodeId(String externalNodeId) {
        if (gameClient != null) {
            for (Map.Entry<Integer, BaseClient> entry : gameClient.getConnectToExternals().entrySet()) {
                if (Objects.equals(entry.getValue().getNodeId(), externalNodeId)) {
                    return entry.getKey();
                }
            }
        }
        return 0;
    }
}
